package com.trg.job.controller;

import com.trg.job.service.FetchDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class FetchDataController  implements SchedulingConfigurer {

    private static final Logger log = LoggerFactory.getLogger(FetchDataController.class);

    // 日数据每日同步cron表达式
    @Value("${cron.day.time}")
    public String DAY_CRON;

    @Autowired
    FetchDataService fetchDataService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        // 配置定时任务异步执行
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);

        taskRegistrar.addTriggerTask(() -> {
            // 定时任务的业务逻辑
            fetchDataService.getZg();
        }, triggerContext -> {
            // 定时任务触发，可修改定时任务的执行周期
            CronTrigger trigger = new CronTrigger(DAY_CRON);
            Date nextExecDate = trigger.nextExecutionTime(triggerContext);
            return nextExecDate;
        });
    }
}
